import { _decorator, Component, EventMouse, EventTouch, Input, input, instantiate, Node, Prefab, size, Size, UITransform, Vec2, Vec3 } from 'cc';
import { gameState } from './gameState';
import { layerAction_001 } from './layerAction-001';
import { eventDispatcher } from './eventDispatcher';
import { editCtlAction } from './editCtlAction';
const { ccclass, property } = _decorator;

@ccclass('editClick')
export class editClick extends Component {
    @property(Prefab) pre_grid: Prefab = null;
    @property(layerAction_001) layer_1: layerAction_001 = null
    @property(editCtlAction) edit_ctl_action:editCtlAction = null;
    cur_grid:Node = null;
    start() {

    }

    update(deltaTime: number) {

    }
    start_edit() {
        this.init_grid();
        this.node.on(Input.EventType.TOUCH_START, this.touch_start, this);
        this.node.on(Input.EventType.TOUCH_MOVE, this.touch_move, this);
        this.node.on(Input.EventType.MOUSE_MOVE, this.mouse_move, this);
    }
    mouse_move(e:EventMouse){
        let wp = e.getUILocation();
        let local_pos = this.node.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(wp.x, wp.y, 0));
        this.edit_ctl_action.set_pos(local_pos);
    }
    touch_move(e: EventTouch){
        let wp = e.getUILocation();
        let local_pos = this.node.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(wp.x, wp.y, 0));
        if(gameState.edit_model !== 1){
            return;
        }
        for (let i = this.node.children.length - 1; i >= 0; i--) {
            this.edit_ctl_action.update_block_size();
            let grid = this.node.children[i];
            if(this.cur_grid === grid)continue;
            if (grid.getComponent(UITransform).getBoundingBox().contains(new Vec2(local_pos.x, local_pos.y))) {
                this.cur_grid = grid;
                this.layer_1.add_block_by_world_pos(grid.getWorldPosition());
                this.layer_1.refresh_shadow();
                break;
            }
        }
    }
    touch_start(e: EventTouch) {
        let wp = e.getUILocation();
        //console.info(wp);
        let local_pos = this.node.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(wp.x, wp.y, 0));
        //console.info(local_pos);
        switch (gameState.edit_model) {
            case 1:
                for (let i = this.node.children.length - 1; i >= 0; i--) {
                    let grid = this.node.children[i];
                    if(this.cur_grid === grid)continue;
                    if (grid.getComponent(UITransform).getBoundingBox().contains(new Vec2(local_pos.x, local_pos.y))) {
                        this.cur_grid = grid;
                        this.layer_1.add_block_by_world_pos(grid.getWorldPosition());
                        this.layer_1.refresh_shadow();
                        break;
                    }
                }
                eventDispatcher.get_target().emit(eventDispatcher.UPDATE_BLOCK_SIZE);
                break;
            case 2:
                this.layer_1.subtract_block(new Vec3(wp.x,wp.y));
                this.layer_1.refresh_shadow();
                eventDispatcher.get_target().emit(eventDispatcher.UPDATE_BLOCK_SIZE);
                break;
            case 3:
                this.add_grid(local_pos.x,local_pos.y,size(10,10));
                break;
            case 4:
                for (let i = this.node.children.length - 1; i >= 0; i--) {
                    let grid = this.node.children[i];
                    if (grid.getComponent(UITransform).getBoundingBox().contains(new Vec2(local_pos.x, local_pos.y))) {
                        grid.removeFromParent()
                        break;
                    }
                }
                break;
        }
    }
    private init_grid() {

        this.node.removeAllChildren();
        const start_x = this.node.getComponent(UITransform).width / 2 * (-1);
        const start_y = this.node.getComponent(UITransform).height / 2 + 10;
        //console.info(start_x+" ,"+start_y);
        for (let i = 0; i < 15; i++) {
            let x = start_x + (38.5 * (i + 1));
            for (let j = 0; j < 17; j++) {
                let y = start_y + (40 * (j + 1) * -1);
                //add grid
                this.add_grid(x, y);
            }
        }
    }
    add_grid(x: number, y: number, size?: Size) {
        let grid = instantiate(this.pre_grid);
        grid.setPosition(x, y);
        grid.setParent(this.node);
        if (size) {
            grid.getComponent(UITransform).setContentSize(size);
        }
    }


}


